package cn.wowjoy.pda.android_pharmacy.common.core;

import android.content.pm.ActivityInfo;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import gear.yc.com.gearlibrary.rxjava.rxbus.RxBus;

public abstract class BaseActivity<DB extends ViewDataBinding, VM extends BaseViewModel, P extends BasePresenter<VM>> extends FragmentActivity {

    protected DB binding;
    protected P presenter;
    protected VM viewModel;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//        SystemBarTintManager tintManager = new SystemBarTintManager(this);
//        tintManager.setStatusBarTintColor(Color.TRANSPARENT);
//        tintManager.setStatusBarTintEnabled(true);
//        tintManager.setHideVirtualKey(getWindow());
//        CommonUtils.closeBar();

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
        RxBus.getInstance().register(this);
        binding = DataBindingUtil.setContentView(this, getLayoutId());

        if (null == presenter) {
            try {
                if (null == getPresenterClass())
                    presenter = null;
                else
                    presenter = getPresenterClass().newInstance();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }

        if (null == viewModel) {
            try {
                if (null == getViewModelClass())
                    viewModel = null;
                else
                    viewModel = getViewModelClass().newInstance();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }

        if (null != presenter && null != viewModel) {
            presenter.setViewModel(viewModel);
        }
        //提前 不然会报空指针
        if (null != presenter) {
            presenter.setContext(this);
            presenter.setActivity(this);
            presenter.onCreate();
//            Log.e("XXX", "presenter.activity: "+presenter.activity.toString() );
        }
        this.init(savedInstanceState);

    }

    @Override
    public void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
    }

    @Override
    protected void onDestroy() {
        if (null != presenter) presenter.onDestroy();
        viewModel = null;
        presenter = null;
        super.onDestroy();
        RxBus.getInstance().unRegister(this);
    }

    protected abstract void init(Bundle savedInstanceState);

    protected abstract int getLayoutId();

    protected abstract Class<P> getPresenterClass();

    protected abstract Class<VM> getViewModelClass();

}
